/**
 * <p>Title: SMSSystemAutoRun.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-6-26 上午06:16:43  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.common.zm.basetools.listener;

import java.util.Calendar;
import java.util.TimerTask;

import javax.servlet.ServletContext;

import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.cInterface.ct.SystemSet;

/**
 * 短信平台的自动活动
 * <p>Title: SMSSystemAutoRun.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-6-26 上午06:16:43  张志芳  Create
 *</pre>
 */
public class SMSSystemAutoRun extends TimerTask{

	@SuppressWarnings("unused")
	private ServletContext context;
	private static boolean isRunning = false;
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	public SMSSystemAutoRun(ServletContext context){
		this.context = context;
	}
	
	/**
	 * 运行
	 * <p>Function: run</p>
	 * <p>Description: </p>
	 * <p>return value: SMSSystemAutoRun</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-26上午06:18:27 张志芳
	 * </p>
	 *
	 */
	@Override  
	public void run() {		
		if("否".equals(SystemSet.getInstance().getFdStart()))//判断是否开启服务，若没有开启服务就不执行定时活动
		{
			return;
		}
		
		if(!isRunning){
			Calendar calPriv = Calendar.getInstance();
			int ihour = calPriv.get(Calendar.HOUR_OF_DAY);
			int iminute = calPriv.get(Calendar.MINUTE);
			if ((ihour == 1) && (iminute < 15)) {
				Runtime.getRuntime().gc();
			}
			isRunning = true;
			
			try {
				this.myTask();
			} catch (Exception e) {
				java.lang.System.err.println("执行任务期间发生异常！" + e.getMessage());
			}
			
			isRunning = false;
		}
	}
	
	/**
	 * 具体的任务
	 * 所有方法必须做异常处理，否则会卡住定时活动
	 * <p>Function: myTask</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-26上午06:19:21 张志芳
	 * </p>
	 *
	 */
	private void myTask(){
		java.lang.System.out.println(">>>> 消息平台开始活动:  .............." + StaticTools.getDateTime());
		
		//定时消息
		//added by zhangzhifang
		//2012-07-01
		RegularSMS regular = new RegularSMS(dao);
		try {
			regular.MakeSMS();
			regular.SendSMS();
		} catch (Exception e) {
			System.out.println("定时活动:  定时消息生成和发送失败！" + e.getMessage());
		}

		//及时消息
		//added by zhangzhifang
		//2012-07-01
		TimelySMS timely = new TimelySMS(dao);
		try {
			timely.SendSMS();
		} catch (Exception e) {
			System.out.println("定时活动:  及时消息发送失败！" + e.getMessage());
		}
		
				
		java.lang.System.out.println(">>>> 消息平台结束活动:  .............." + StaticTools.getDateTime());
	}
}
